package cn.bincker.mycaptcha.utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

public class ImageUtils {
    private static final int GENERAL_IMAGE_LENGTH = 1024 * 20;//20k

    public static String convertImageToBase64(BufferedImage image, String format){
        try{
            var out = new ByteArrayOutputStream();
            ImageIO.write(image, format, out);
            return "data:image/" + format + ";base64," + Base64.getEncoder().encodeToString(out.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException("输出图片", e);
        }
    }
}
